package com.sun.controller;

import com.sun.pojo.Comment;
import com.sun.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author: glorysun
 * @date: 2020/8/10
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Value("${comment.youke}")
    private String avatar;

    @GetMapping("/comments/{blogId}")
    public String commentList(@PathVariable("blogId") Long blogId, Model model){
        model.addAttribute("comments",commentService.selectCommentByBlogId(blogId));
        return "redirect:/blog/"+blogId;
    }

    @PostMapping("/comments")
    public String reply(Comment comment){
        comment.setAvatar(avatar);
        commentService.insertComment(comment);
        return "redirect:/comments/"+comment.getBlogId();
    }
}
